using System;
using System.Collections.Generic;
using Volo.Abp.Application.Dtos;
using Matrix.ServiceDiscovery.Enums;

namespace Matrix.ServiceDiscovery.Services
{
    /// <summary>
    /// 服务实例DTO
    /// </summary>
    public class ServiceInstanceDto : FullAuditedEntityDto<Guid>
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 实例ID
        /// </summary>
        public string InstanceId { get; set; } = string.Empty;

        /// <summary>
        /// 服务版本
        /// </summary>
        public string? Version { get; set; }

        /// <summary>
        /// 主机地址
        /// </summary>
        public string Host { get; set; } = string.Empty;

        /// <summary>
        /// 端口号
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// 协议
        /// </summary>
        public string Protocol { get; set; } = "HTTP";

        /// <summary>
        /// 服务地址
        /// </summary>
        public string ServiceAddress => $"{Protocol.ToLower()}://{Host}:{Port}";

        /// <summary>
        /// 元数据
        /// </summary>
        public Dictionary<string, object>? Metadata { get; set; }

        /// <summary>
        /// 权重
        /// </summary>
        public int Weight { get; set; } = 1;

        /// <summary>
        /// 服务状态
        /// </summary>
        public ServiceStatus Status { get; set; } = ServiceStatus.Unknown;

        /// <summary>
        /// 健康状态
        /// </summary>
        public HealthStatus HealthStatus { get; set; } = HealthStatus.Unknown;

        /// <summary>
        /// 最后健康检查时间
        /// </summary>
        public DateTime? LastHealthCheckTime { get; set; }

        /// <summary>
        /// 注册时间
        /// </summary>
        public DateTime RegisterTime { get; set; }

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime? LastUpdateTime { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpirationTime { get; set; }

        /// <summary>
        /// 区域
        /// </summary>
        public string? Region { get; set; }

        /// <summary>
        /// 可用区
        /// </summary>
        public string? Zone { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public List<string>? Tags { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;

        /// <summary>
        /// 是否过期
        /// </summary>
        public bool IsExpired => ExpirationTime.HasValue && ExpirationTime.Value < DateTime.Now;

        /// <summary>
        /// 运行时长
        /// </summary>
        public TimeSpan? RunningDuration => DateTime.Now - RegisterTime;

        /// <summary>
        /// 距离过期时间
        /// </summary>
        public TimeSpan? TimeToExpiration => ExpirationTime.HasValue ? ExpirationTime.Value - DateTime.Now : null;
    }

    /// <summary>
    /// 创建服务实例DTO
    /// </summary>
    public class CreateServiceInstanceDto
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 实例ID
        /// </summary>
        public string InstanceId { get; set; } = string.Empty;

        /// <summary>
        /// 服务版本
        /// </summary>
        public string? Version { get; set; }

        /// <summary>
        /// 主机地址
        /// </summary>
        public string Host { get; set; } = string.Empty;

        /// <summary>
        /// 端口号
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// 协议
        /// </summary>
        public string Protocol { get; set; } = "HTTP";

        /// <summary>
        /// 元数据
        /// </summary>
        public Dictionary<string, object>? Metadata { get; set; }

        /// <summary>
        /// 权重
        /// </summary>
        public int Weight { get; set; } = 1;

        /// <summary>
        /// 区域
        /// </summary>
        public string? Region { get; set; }

        /// <summary>
        /// 可用区
        /// </summary>
        public string? Zone { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public List<string>? Tags { get; set; }

        /// <summary>
        /// 过期时间（秒）
        /// </summary>
        public int? TtlSeconds { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;
    }

    /// <summary>
    /// 更新服务实例DTO
    /// </summary>
    public class UpdateServiceInstanceDto
    {
        /// <summary>
        /// 服务版本
        /// </summary>
        public string? Version { get; set; }

        /// <summary>
        /// 元数据
        /// </summary>
        public Dictionary<string, object>? Metadata { get; set; }

        /// <summary>
        /// 权重
        /// </summary>
        public int? Weight { get; set; }

        /// <summary>
        /// 区域
        /// </summary>
        public string? Region { get; set; }

        /// <summary>
        /// 可用区
        /// </summary>
        public string? Zone { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public List<string>? Tags { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool? IsEnabled { get; set; }

        /// <summary>
        /// 延长TTL（秒）
        /// </summary>
        public int? ExtendTtlSeconds { get; set; }
    }

    /// <summary>
    /// 服务注册DTO
    /// </summary>
    public class ServiceRegistrationDto : CreationAuditedEntityDto<Guid>
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 服务组
        /// </summary>
        public string? ServiceGroup { get; set; }

        /// <summary>
        /// 服务类型
        /// </summary>
        public ServiceType ServiceType { get; set; } = ServiceType.WebApi;

        /// <summary>
        /// 服务版本
        /// </summary>
        public string? Version { get; set; }

        /// <summary>
        /// 服务描述
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 服务所有者
        /// </summary>
        public string? Owner { get; set; }

        /// <summary>
        /// 联系方式
        /// </summary>
        public string? Contact { get; set; }

        /// <summary>
        /// 服务配置
        /// </summary>
        public Dictionary<string, object>? Configuration { get; set; }

        /// <summary>
        /// 负载均衡策略
        /// </summary>
        public LoadBalancingStrategy LoadBalancingStrategy { get; set; } = LoadBalancingStrategy.RoundRobin;

        /// <summary>
        /// 健康检查配置
        /// </summary>
        public HealthCheckConfigDto? HealthCheckConfig { get; set; }

        /// <summary>
        /// 服务发现配置
        /// </summary>
        public Dictionary<string, object>? DiscoveryConfig { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public List<string>? Tags { get; set; }

        /// <summary>
        /// 环境
        /// </summary>
        public string? Environment { get; set; }

        /// <summary>
        /// 区域
        /// </summary>
        public string? Region { get; set; }

        /// <summary>
        /// 可用区
        /// </summary>
        public string? Zone { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;

        /// <summary>
        /// 是否需要认证
        /// </summary>
        public bool RequiresAuthentication { get; set; } = false;

        /// <summary>
        /// 是否启用TLS
        /// </summary>
        public bool EnableTls { get; set; } = false;

        /// <summary>
        /// 超时时间（秒）
        /// </summary>
        public int TimeoutSeconds { get; set; } = 30;

        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; } = 3;
    }

    /// <summary>
    /// 创建服务注册DTO
    /// </summary>
    public class CreateServiceRegistrationDto
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 服务组
        /// </summary>
        public string? ServiceGroup { get; set; }

        /// <summary>
        /// 服务类型
        /// </summary>
        public ServiceType ServiceType { get; set; } = ServiceType.WebApi;

        /// <summary>
        /// 服务版本
        /// </summary>
        public string? Version { get; set; }

        /// <summary>
        /// 服务描述
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 服务所有者
        /// </summary>
        public string? Owner { get; set; }

        /// <summary>
        /// 联系方式
        /// </summary>
        public string? Contact { get; set; }

        /// <summary>
        /// 服务配置
        /// </summary>
        public Dictionary<string, object>? Configuration { get; set; }

        /// <summary>
        /// 负载均衡策略
        /// </summary>
        public LoadBalancingStrategy LoadBalancingStrategy { get; set; } = LoadBalancingStrategy.RoundRobin;

        /// <summary>
        /// 健康检查配置
        /// </summary>
        public HealthCheckConfigDto? HealthCheckConfig { get; set; }

        /// <summary>
        /// 服务发现配置
        /// </summary>
        public Dictionary<string, object>? DiscoveryConfig { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public List<string>? Tags { get; set; }

        /// <summary>
        /// 环境
        /// </summary>
        public string? Environment { get; set; }

        /// <summary>
        /// 区域
        /// </summary>
        public string? Region { get; set; }

        /// <summary>
        /// 可用区
        /// </summary>
        public string? Zone { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;

        /// <summary>
        /// 是否需要认证
        /// </summary>
        public bool RequiresAuthentication { get; set; } = false;

        /// <summary>
        /// 是否启用TLS
        /// </summary>
        public bool EnableTls { get; set; } = false;

        /// <summary>
        /// 超时时间（秒）
        /// </summary>
        public int TimeoutSeconds { get; set; } = 30;

        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; } = 3;
    }

    /// <summary>
    /// 更新服务注册DTO
    /// </summary>
    public class UpdateServiceRegistrationDto
    {
        /// <summary>
        /// 服务组
        /// </summary>
        public string? ServiceGroup { get; set; }

        /// <summary>
        /// 服务版本
        /// </summary>
        public string? Version { get; set; }

        /// <summary>
        /// 服务描述
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 服务所有者
        /// </summary>
        public string? Owner { get; set; }

        /// <summary>
        /// 联系方式
        /// </summary>
        public string? Contact { get; set; }

        /// <summary>
        /// 服务配置
        /// </summary>
        public Dictionary<string, object>? Configuration { get; set; }

        /// <summary>
        /// 负载均衡策略
        /// </summary>
        public LoadBalancingStrategy? LoadBalancingStrategy { get; set; }

        /// <summary>
        /// 健康检查配置
        /// </summary>
        public HealthCheckConfigDto? HealthCheckConfig { get; set; }

        /// <summary>
        /// 服务发现配置
        /// </summary>
        public Dictionary<string, object>? DiscoveryConfig { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public List<string>? Tags { get; set; }

        /// <summary>
        /// 环境
        /// </summary>
        public string? Environment { get; set; }

        /// <summary>
        /// 区域
        /// </summary>
        public string? Region { get; set; }

        /// <summary>
        /// 可用区
        /// </summary>
        public string? Zone { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool? IsEnabled { get; set; }

        /// <summary>
        /// 是否需要认证
        /// </summary>
        public bool? RequiresAuthentication { get; set; }

        /// <summary>
        /// 是否启用TLS
        /// </summary>
        public bool? EnableTls { get; set; }

        /// <summary>
        /// 超时时间（秒）
        /// </summary>
        public int? TimeoutSeconds { get; set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public int? RetryCount { get; set; }
    }
}